package com.oa.hyperion.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oa.hyperion.entity.DepartmentMessageMerge;
import com.oa.hyperion.entity.Employee;
import com.oa.hyperion.entity.EmployeeMessageMerge;
import com.oa.hyperion.entity.Information;
import com.oa.hyperion.repository.msg.DepartmentMessageMergeMapper;
import com.oa.hyperion.repository.msg.EmployeeMessageMergeMapper;
import com.oa.hyperion.repository.msg.InformationMapper;
import com.oa.hyperion.service.DepartmentMessageMergeService;
import com.oa.hyperion.service.EmployeeMessageMergeService;
import com.oa.hyperion.service.EmployeeService;
import com.oa.hyperion.service.InformationService;
import com.oa.hyperion.vo.InformationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author Liu
 * @date 2022年01月17日 14:02
 */
@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements InformationService {
    @Autowired
    private DepartmentMessageMergeService depMsgService;
    @Autowired
    private EmployeeMessageMergeService empMsgService;
    @Autowired(required = true)
    private InformationMapper informationMapper;
    @Autowired
    private EmployeeMessageMergeMapper employeeMessageMergeMapper;
    @Autowired
    private DepartmentMessageMergeMapper departmentMessageMergeMapper;
    @Autowired
    private EmployeeService employeeService;
    @Override
    public Information getById(Map<String, Object> map) {
        Information byId = informationMapper.getById(map);
        return  byId;
    }

    @Override
    public boolean save(InformationVo informationVo) {
        Information entity = informationVo.getInformation();
        Employee byId = employeeService.getById(informationVo.getCreatorId());
        entity.setSender(byId.getName());
        entity.setCreateTime(new Date());
        entity.setRea("m");
        boolean flag = false;
        if (entity.getStatus() == null) {
            entity.setStatus("m");
            entity.setPublishTime(null);
            flag=super.save(entity);
        } else {
            entity.setPublishTime(new Date());
            flag=super.save(entity);
            if (entity.getTypes().equals("g")) {
                DepartmentMessageMerge departmentMessageMerge = new DepartmentMessageMerge();
                departmentMessageMerge.setDepartmentId(informationVo.getId());
                departmentMessageMerge.setCreatorId(informationVo.getCreatorId());
                departmentMessageMerge.setInformationId(entity.getId());
                depMsgService.save(departmentMessageMerge);
            } else {
                EmployeeMessageMerge employeeMessageMerge = new EmployeeMessageMerge();
                employeeMessageMerge.setEmployeeId(informationVo.getId());
                employeeMessageMerge.setEmployeeId(entity.getId());
                employeeMessageMerge.setCreatorId(informationVo.getCreatorId());
                empMsgService.save(employeeMessageMerge);
            }
        }
        return flag;
    }
}
